<?php
require_once Yii::app()->basePath . "/extensions/phpquery/phpQuery/phpQuery.php";

/**
 * 爬取
 * @author xiaojy xiaojy@wanthings.com
 */
class CrawleCommand extends CConsoleCommand
{
    public function actionBegin()
    {
        $num = 1;
        while (1) {
            $this->getData();
            sleep(0.01);
            echo "end " . $num . " time\n";
            $num++;
        }
    }

    public function getData()
    {

        for ($num = 1; $num < 10; $num++) {
            $url = 'https://coinmarketcap.com/' . $num;
            //$url = 'https://www.baidu.com';
            $UserAgent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36';
            $ch = curl_init();


            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            $data = curl_exec($ch);

            if ($data == false) {
                continue;
            }
            curl_close($ch);


            phpQuery::newDocumentHTML($data);//加载页面
            //能否找到 此条url .是否已查找 。已查找一条未查的数据，开始查找。
            //如果能，找过了
            //所有的链接
            $log = array();
            $log['yes'] = 0;
            $log['no'] = 0;
            foreach (pq("#currencies tbody tr ") as $key => $value) {
                //获取href。
                $temp = pq($value);
                //  echo $temp;
                $data = array();
                $temp = pq(".currency-symbol", $value)->text();
                //  $temp = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $temp);
                $data['short_name'] = DigitalCashLog::model()->myTrim($temp);

                $temp = pq(".currency-name-container", $value)->text();

                $data['name'] = DigitalCashLog::model()->myTrim($temp);

                $temp = pq(".market-cap", $value)->text();    //市值
                $temp = DigitalCashLog::model()->number($temp);
                $data['market_cap'] = DigitalCashLog::model()->myTrim($temp);


                $temp = pq(".price", $value)->text();
                $data['usd_price'] = DigitalCashLog::model()->myTrim($temp);

                $temp = pq(".volume", $value)->text();
                $temp = DigitalCashLog::model()->number($temp);
                $data['volume'] = DigitalCashLog::model()->myTrim($temp);

                $temp = pq(".circulating-supply", $value)->text(); //供应

                $temp = DigitalCashLog::model()->myTrim($temp);
                $data['supply'] = $temp;

                $temp = pq(".text-center", $value)->text();    //排名
                $data['ranking'] = $temp;

                $temp = pq("img", $value)->attr("src"); //图片链接
                $data['sparkline'] = $temp;

                $temp = pq(".percent-24h", $value)->text();
                $temp = DigitalCashLog::model()->myTrim($temp);
                $data['day_change'] = $temp;

                $result = DigitalCashLog::model()->add($data);    //是
                $log['time'] = time();
                $log['num'] = $num;
                if ($result['errno'] == 0) {
                    $log['yes'] += 1;    //
                } else {
                    $log['no'] += 1;    //
                    $log['data'] = $data;
                }
                // $temp = htmlspecialchars_decode($temp);
                // $temp = preg_replace("/<a[^>]*>(.*?)<\/a>/is", "$1", $temp);
                // $temp = DigitalCashLog::model()->myTrim($temp);    //处理掉美元符号与逗号
                //echo $temp;

            }
            file_put_contents(Yii::app()->getRuntimePath() . '/digital_' . date("Y_W") . '.txt', sprintf("%s %s %s", date('Y-m-d H:i:s'), CJSON::encode($log), PHP_EOL), FILE_APPEND);
        }

    }

}